package com.ktjy.pojo.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;

/**
 * @Description
 * @Author 李楚鹏
 * @Date 2024/11/18 18:38
 **/

public class MyBatisUtil {
    private static SqlSessionFactory factory;

    static{
        try{
            InputStream is= Resources.getResourceAsStream("mybatis-config.xml");
            factory=new SqlSessionFactoryBuilder().build(is);
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

    public static SqlSession createSqlsession(){
        return factory.openSession(false);
    }

    public static void  closeSqlSession(SqlSession sqlSession){
        if (sqlSession!=null)
            sqlSession.close();
    }
}
